package com.maimao.auth.imageCode.email;

import com.maimao.auth.imageCode.AbstractPwdCodeTokenGranter;
import com.maimao.core.constant.GrantTypeEnum;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

/**
 * 用户名密码+图形验证码授权方式
 *
 * @author MaoLin Wang
 * @date 2020/12/14 3:15 下午
 */
public class EmailPwdCodeTokenGranter extends AbstractPwdCodeTokenGranter {

    private static final String GRANT_TYPE = GrantTypeEnum.EMAIL_PWD_CODE.getType();
    private static final int CODE_LENGTH = 5;
    private final AuthenticationManager authenticationManager;

    public EmailPwdCodeTokenGranter(AuthorizationServerTokenServices tokenServices,
                                    ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, final AuthenticationManager authenticationManager) {
        super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
        this.authenticationManager = authenticationManager;
    }


    @Override
    protected Integer getCodeLength() {
        return CODE_LENGTH;
    }

    @Override
    protected Authentication createAuthenticationToken(String username, String password, String code,String uuid) {
        return new EmailPwdCodeAuthenticationToken(username,password,code,uuid);
    }

    @Override
    protected AuthenticationManager authenticationManager() {
        return this.authenticationManager;
    }


}
